#include <iostream>
#include <sstream>
#include <cstring>
#include <cerrno>
#include <atomic>
#include <unistd.h>
#include "writer.h"

void write_TSNConfig(TSNConfig *tsnconfig, Tunnel *tunnel) {
    int i = 0;
    // std::cout << "2" << std::endl;
    // printf("")
    for (; i < tunnel->TC_num; i++) {
        if (!std::strcmp(tunnel->TC[i].entity, "device_id")) {
            tsnconfig->device_id = std::stoi(tunnel->TC[i].content);
            std::strcpy(tunnel->TC[i].content, "OK");
        } else if (!std::strcmp(tunnel->TC[i].entity, "gm_flag")) {
            tsnconfig->gm_flag = std::stoi(tunnel->TC[i].content);
            std::strcpy(tunnel->TC[i].content, "OK");
        } else if (!std::strcmp(tunnel->TC[i].entity, "port_num")) {
            tsnconfig->port_num = std::stoi(tunnel->TC[i].content);
            std::strcpy(tunnel->TC[i].content, "OK");
        } else if (!std::strcmp(tunnel->TC[i].entity, "master_port")) {
            tsnconfig->master_port = std::stoi(tunnel->TC[i].content);
            std::strcpy(tunnel->TC[i].content, "OK");
        } else if (!std::strcmp(tunnel->TC[i].entity, "slave_port")) {
            tsnconfig->slave_port = std::stoi(tunnel->TC[i].content);
            std::strcpy(tunnel->TC[i].content, "OK");
        } else if (!std::strcmp(tunnel->TC[i].entity, "link_delay_measure_period")) {
            tsnconfig->link_delay_measure_period = std::stoi(tunnel->TC[i].content);
            std::strcpy(tunnel->TC[i].content, "OK");
        } else if (!std::strcmp(tunnel->TC[i].entity, "sync_period")) {
            tsnconfig->sync_period = std::stoi(tunnel->TC[i].content);
            std::strcpy(tunnel->TC[i].content, "OK");
        } else if (!std::strcmp(tunnel->TC[i].entity, "domain_number")) {
            tsnconfig->domain_number = std::stoi(tunnel->TC[i].content);
            std::strcpy(tunnel->TC[i].content, "OK");
        } else if (!std::strcmp(tunnel->TC[i].entity, "mean_link_delay_thrld")) {
            tsnconfig->mean_link_delay_thrld = std::stoi(tunnel->TC[i].content);
            std::strcpy(tunnel->TC[i].content, "OK");
        } else {
            std::strcpy(tunnel->TC[i].content, "error");
        }
    }
    int j = 0;
    for (; j < tunnel->PI.ci_num; j++) {
        tsnconfig->port_identity.clockIdentity[j] = static_cast<uint8_t>(std::strtol(tunnel->PI.ci[j].clockIdentity, nullptr, 10));
    }
    if (tunnel->PI.content != nullptr) {
        tsnconfig->port_identity.portNumber = std::stoi(tunnel->PI.content);
    }
}
